home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / DTS QT Utilities.Aug-95 / Projects & Test Apps / QT Internals / Mac Framework / MacFramework.c < prev    next >
Encoding:
Text File  |  1995-05-09  |  20.2 KB  |  822 lines  |  [TEXT/MPCC]

  1. /*
  2.     File:        MacFramework.c
  3.  
  4.     Contains:    Basic Macintosh Functions for Window, Menu handling and similar things.
  5.  
  6.     Written by:    DTS
  7.  
  8.     Copyright:    © 1994-1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.        <1>         12/20/94    khs        first file
  13.        
  14. */
  15.  
  16.  
  17. // INCLUDES
  18. #include <SegLoad.h>
  19. #include <ToolUtils.h>
  20. #include <Devices.h>
  21. #include <Fonts.h>
  22.  
  23. #include "DTSQTUtilities.h"
  24. #include "AppConfiguration.h"
  25. #include "MacFramework.h"
  26.  
  27.  
  28. // WINDOW DEFINITIONS
  29. const Rect kDefaultWinRect;
  30. Rect kLimitRect = {0, 0, 480, 640};                            //max size for any window
  31. long gMCFlags = kMCFlags;
  32.  
  33.  
  34. // GLOBALS
  35. Boolean gQuitFlag = false;                                        // Flag that keeps track of termination state.
  36. unsigned long gWNEsleep = kWNEDefaultSleep;        // WaitNextEvent sleep time.
  37. Str255 gWindowTitle = "\pUntitled";                    // Default name for created windows.
  38.  
  39.  
  40. // PURE MAC TOOLBOX FUNCTIONS
  41.  
  42. // ______________________________________________________________________
  43. void InitMacEnvironment(long nMasters)
  44. {
  45.     long i;
  46.     MaxApplZone();
  47.     
  48.     for(i = 0; i <nMasters; i++)
  49.         MoreMasters();
  50.     
  51.     InitGraf(&qd.thePort);
  52.     InitFonts();
  53.     InitWindows();
  54.     InitMenus();
  55.     FlushEvents(everyEvent, 0);
  56.     TEInit();
  57.     InitCursor();
  58.     InitDialogs(NULL);
  59. }
  60.  
  61.  
  62. // ______________________________________________________________________
  63. void InitStack(long extraStackSpace)
  64. {
  65.     Ptr size = GetApplLimit();
  66.     SetApplLimit(size - extraStackSpace);    // make room on the stack
  67. }
  68.  
  69.  
  70. // ______________________________________________________________________
  71. Boolean InitMenubar(void)
  72. {
  73.     Handle aMenuHandle = NULL;
  74.     
  75.     aMenuHandle = GetNewMBar(mMenubar); DebugAssert(aMenuHandle != NULL);
  76.     if(aMenuHandle == NULL)
  77.     {
  78.         ShowWarning("\pCould not find the Menubar resource!", 0);
  79.         return false;
  80.     }
  81.     
  82.     SetMenuBar(aMenuHandle);
  83.     DisposeHandle(aMenuHandle);  DebugAssert(MemError() == noErr);
  84.     
  85.     AddResMenu(GetMHandle(mApple), 'DRVR');
  86.  
  87.     DrawMenuBar();
  88.     return true;
  89. }
  90.  
  91.  
  92. // ______________________________________________________________________
  93. void HandleMenuCommand(long theMenuResult)
  94. {
  95.     short             aMenuID, aMenuItem;
  96.     Str255            daName;
  97.     
  98.     aMenuID = HiWord(theMenuResult);
  99.     aMenuItem = LoWord(theMenuResult);
  100.     
  101.     switch(aMenuID)
  102.     {
  103.         // APPLE MENU
  104.         case mApple:
  105.             switch(aMenuItem)
  106.             {
  107.                 case iAbout:    // about box
  108.                     ShowAboutDialogBox();     
  109.                     break;
  110.                 
  111.                 default:     // Apple menu handling
  112.                     GetItem(GetMHandle(mApple), aMenuItem, daName);
  113.                     (void)OpenDeskAcc(daName);
  114.                     break;
  115.             } // end switch(aMenuItem)
  116.             break;
  117.  
  118.         // FILE MENU            
  119.         case mFile:
  120.             switch(aMenuItem)
  121.             {
  122.                 case iNew:
  123.                     {
  124.                         if ( !DoCreateNewMovie())
  125.                         {
  126.                             SysBeep(kDefaultSysBeep);
  127.                             ShowWarning("\pCould not create a new movie!", 0);
  128.                             break;
  129.                         }
  130.                     }
  131.                     break;
  132.                     
  133.                 case iOpen:
  134.                     if (!DoCreateMovieWindow( NULL) )
  135.                     {
  136.                         ShowWarning("\pCould not create a new movie window!", 0);
  137.                         SysBeep(kDefaultSysBeep); 
  138.                         break;
  139.                     }
  140.                     break;
  141.                 
  142.                 case iClose:
  143.                     DoDestroyMovieWindow( FrontWindow() );
  144.                     break;
  145.                 
  146.                 case iSave:
  147.                     {
  148.                         if( !DoUpdateMovieFile( FrontWindow()) )
  149.                         {
  150.                             SysBeep(kDefaultSysBeep);
  151.                             ShowWarning("\pCould not save the movie file!", 0);
  152.                              break;
  153.                         }
  154.                     }
  155.                     break;
  156.                     
  157.                 case iSaveAs:
  158.                     {
  159.                         MovieController mc;
  160.                     
  161.                         mc = GetMCFromFrontWindow();
  162.                         if(mc == NULL) 
  163.                         {
  164.                             SysBeep(kDefaultSysBeep); 
  165.                             break;
  166.                         }
  167.                         
  168.                          if( QTUSaveMovie(MCGetMovie(mc)) != noErr)
  169.                          {
  170.                              SysBeep(kDefaultSysBeep);
  171.                              ShowWarning("\pCould not save the movie file!", 0);
  172.                              break;
  173.                          }
  174.                      }
  175.                     break;
  176.                                     
  177.                 case iPrint:
  178.                     {
  179.                         MovieController mc;
  180.                         OSErr anErr = noErr;
  181.  
  182.                         mc = GetMCFromFrontWindow();
  183.                         if(mc != NULL)
  184.                         {
  185.                             anErr = QTUPrintMoviePICT( MCGetMovie(mc), kDefaultX, kDefaultY, kPrintFrame); 
  186.                             if(anErr != noErr)
  187.                             {
  188.                                 ShowWarning("\pCould not print!", anErr);
  189.                                 SysBeep(kDefaultSysBeep);
  190.                             }
  191.                         }
  192.                         else
  193.                                 SysBeep(kDefaultSysBeep);
  194.                         break;
  195.                     }
  196.  
  197.                 case iQuit:
  198.                     {
  199.                         gQuitFlag = true;
  200.                         break;
  201.                     }
  202.  
  203.             } // end switch(aMenuItem), mFile
  204.             break;
  205.     
  206.         
  207.         // EDIT MENU
  208.         // Provide the default controller cut, copy and paste functionality.    
  209.         case mEdit:
  210.         {
  211.             Movie aMovie = NULL;
  212.             MovieController mc;
  213.             
  214.             mc = GetMCFromFrontWindow();
  215.             if (mc == NULL) break;
  216.             
  217.             switch(aMenuItem)
  218.             {
  219.                 case iUndo: MCUndo(mc); break;
  220.                 
  221.                 case iCut: aMovie = MCCut(mc); break;
  222.                 
  223.                 case iCopy: aMovie = MCCopy(mc); break;
  224.                 
  225.                 case iPaste: MCPaste(mc, NULL); break;
  226.                 
  227.                 case iClear: MCClear(mc); break;
  228.                 
  229.                 case iSelectAll:  
  230.                     if(QTUSelectAllMovie(mc) != noErr)
  231.                         SysBeep(kDefaultSysBeep);
  232.                     break;
  233.             } // end switch(aMenuItem)
  234.             
  235.             if(aMovie)
  236.             {
  237.                 PutMovieOnScrap(aMovie, 0);
  238.                 DisposeMovie(aMovie);  DebugAssert(MemError() == noErr);
  239.             }
  240.             break;
  241.         } // end case mEdit
  242.  
  243.  
  244.     default:
  245.         HandleApplicationMenu(aMenuID, aMenuItem);
  246.         break;
  247.     } // end switch(aMenuID)
  248.     
  249.     HiliteMenu(0);
  250. }
  251.  
  252.  
  253. // ______________________________________________________________________
  254. void AdjustMenus(void)
  255. {
  256.     WindowRef             aWindow;
  257.     MovieController    mc;
  258.     WindowObject        aWindowObject;
  259.     
  260.     aWindow = FrontWindow();
  261.  
  262.     if(aWindow != NULL)
  263.     {
  264.         // Enable the close entry of we have windows = movies.
  265.         EnableItem( GetMHandle(mFile), iClose);
  266.         
  267.         // Handle the edit menu.
  268.         if( (aWindowObject = (WindowObject)GetWRefCon(aWindow) ) != NULL)
  269.         {
  270.             mc = (**aWindowObject).controller;
  271.             if( (IsWindowObjectOurs(aWindowObject)) && (mc != NULL))
  272.             {
  273.                 MCSetUpEditMenu(mc, 0L, GetMHandle(mEdit));
  274.                 EnableItem(GetMHandle(mEdit), iSelectAll);
  275.         
  276.                 EnableItem(GetMHandle(mFile), iSave);
  277.                 EnableItem(GetMHandle(mFile), iSaveAs);
  278.                 EnableItem(GetMHandle(mFile), iClose);
  279.                 EnableItem(GetMHandle(mFile), iPrint);
  280.             }
  281.         }
  282.     } // end if(aWindow != NULL)
  283.     else 
  284.     {
  285.         DisableItem(GetMHandle(mFile), iSave);
  286.         DisableItem(GetMHandle(mFile), iSaveAs);
  287.         DisableItem(GetMHandle(mFile), iClose);
  288.         DisableItem(GetMHandle(mFile), iPrint);
  289.         
  290.         DisableItem(GetMHandle(mEdit), iCut);
  291.         DisableItem(GetMHandle(mEdit), iCopy);
  292.         DisableItem(GetMHandle(mEdit), iPaste);
  293.         DisableItem(GetMHandle(mEdit), iUndo);
  294.         DisableItem(GetMHandle(mEdit), iClear);
  295.         DisableItem(GetMHandle(mEdit), iSelectAll);
  296.         
  297.     }
  298.     
  299.     AdjustApplicationMenus();                    // fix any specific app menus as well.
  300. }
  301.  
  302.  
  303. // ______________________________________________________________________
  304. void MainEventLoop(void)
  305. {
  306.     EventRecord             anEvent;
  307.     WindowRef            whichWindow, aWindow;
  308.     Boolean                    aMovieEvent;
  309.     short                    aWindowPart;
  310.     Rect                        aScreenRect;
  311.     Rect                        aRefreshArea;
  312.     Point                        aPoint  = {100, 100};
  313.     WindowObject        aWindowObject;
  314.     MovieController    mc;
  315.     
  316.     while(!gQuitFlag)
  317.     {
  318.         WaitNextEvent(everyEvent, &anEvent, gWNEsleep, NULL);
  319.         
  320. #ifdef USESIOUX
  321.         SIOUXHandleOneEvent(&anEvent);
  322. #endif USESIOUX
  323.  
  324.         AdjustMenus();
  325.         aMovieEvent = false;
  326.         
  327.         if( (whichWindow = FrontWindow() ) != NULL)
  328.             DoIdle(whichWindow);
  329.  
  330.         // First, let the movie controller have access to the event.
  331.         for( aWindow = FrontWindow(); aWindow != NULL ; aWindow = (WindowPtr)((WindowPeek)aWindow)->nextWindow)
  332.             if(( aWindowObject = (WindowObject)GetWRefCon(aWindow)) != NULL)         
  333.                 if((IsWindowObjectOurs(aWindowObject)) && ( (mc = (**aWindowObject).controller) != NULL) ) 
  334.                     if(MCIsPlayerEvent(mc, &anEvent)) 
  335.                         aMovieEvent = true ;
  336.                     
  337.     // Then, if this wasn't a movie controller event, pass it on to the case statement that dispatches the event
  338.     // to the right function.
  339.     if(!aMovieEvent)
  340.     {
  341.         switch(anEvent.what)
  342.         {
  343.             case mouseDown:
  344.                 aWindowPart = FindWindow(anEvent.where, &whichWindow);
  345.  
  346.                 // Window related events:            
  347.                 switch(aWindowPart)
  348.                 {
  349.                     case inMenuBar:
  350.                         HandleMenuCommand(MenuSelect(anEvent.where));
  351.                         break;
  352.                         
  353.                     case inDrag:
  354.                     {
  355.                         Rect                         aRect;
  356.                         Movie                    aMovie = NULL;
  357.                         MovieController     mc = NULL;
  358.                         WindowObject         aWindowObject = NULL;
  359.                         
  360.                         aWindowObject = (WindowObject)GetWRefCon(whichWindow);
  361.                         mc = (**aWindowObject).controller;
  362.                         if (! (IsWindowObjectOurs(aWindowObject)) && (mc == NULL))
  363.                             break;
  364.                             
  365.                         aMovie = MCGetMovie(mc);
  366.                         
  367.                         GetMovieBox(aMovie, &aRect);
  368.                         aScreenRect = (**GetGrayRgn()).rgnBBox;
  369.                         DragAlignedWindow(whichWindow, anEvent.where, &aScreenRect, &aRect, NULL);
  370.                     }  // end case inDrag;    
  371.                         break;
  372.                         
  373.                     case inContent:
  374.                         SelectWindow(whichWindow);
  375.                         HandleContentClick(whichWindow, &anEvent);
  376.                         break;
  377.                     
  378.                     case inGoAway:
  379.                         // if the window is closed, dispose the movie, the controller and the window
  380.                         if( TrackGoAway(whichWindow, anEvent.where) )
  381.                             DoDestroyMovieWindow(whichWindow);
  382.                         break;
  383.                 } // end switch(aWindowPart):
  384.                 break;
  385.  
  386.                 // System level events:
  387.                 case updateEvt:
  388.                     whichWindow = (WindowRef)anEvent.message;
  389.                     aRefreshArea = ((**(whichWindow->visRgn)).rgnBBox);
  390.                     DoUpdateWindow(whichWindow, &aRefreshArea);
  391.                     break;
  392.                     
  393.                 case keyDown:
  394.                 case autoKey:
  395.                     HandleKeyPress(&anEvent);
  396.                     break;
  397.                 
  398.                 case diskEvt:
  399.                     if(HiWord(anEvent.message) != noErr)
  400.                         (void)DIBadMount(aPoint, anEvent.message);
  401.                     break;
  402.                 
  403.                 case activateEvt:
  404.                     whichWindow = (WindowRef)anEvent.message;
  405.                     
  406.                      if ( IsAppWindow(whichWindow) )
  407.                     {
  408.                         DoActivateWindow(whichWindow, ((anEvent.modifiers & activeFlag) != 0 ));
  409.                     }
  410.                     break;
  411.                     
  412.                 case osEvt:
  413.                     switch(( anEvent.message > 24) & 0x00FF )        // get high byte of word
  414.                     {
  415.                         case suspendResumeMessage:
  416.                             if( FrontWindow() )
  417.                             {
  418.                                 DoActivateWindow(FrontWindow(), !((anEvent.message & resumeFlag) == 0));
  419.                             }
  420.                             break;
  421.                         
  422.                         case mouseMovedMessage:
  423.                             break;
  424.                     } // end switch(anEvent.message > 24) & 0x00FF)    
  425.                     break;
  426.                 
  427.                 case nullEvent:
  428.                     if(( whichWindow = FrontWindow() ) != NULL)
  429.                         DoIdle(whichWindow);
  430.                     break;
  431.         } // end switch(anEvent.what)
  432.     } // end if(!aMovieEvent)    
  433.     } // end while(!gQuitFlag)
  434. }
  435.  
  436.  
  437. // ______________________________________________________________________
  438. Boolean IsAppWindow(WindowRef theWindow)
  439. {
  440.     short aWindowKind;
  441.     
  442.     if (theWindow == NULL)
  443.         return false;
  444.     else
  445.     {
  446.         aWindowKind = ((WindowPeek)theWindow)->windowKind;
  447.         return ( (aWindowKind >= userKind) || (aWindowKind == dialogKind) );
  448.     }
  449. }
  450.  
  451.  
  452. // ______________________________________________________________________
  453. WindowObject CreateWindowObject(WindowRef theWindow)
  454. {
  455.     WindowObject aWindowObject = NULL;
  456.     
  457.     // WindowObjectRecord = 90 bytes (good to know if chasing for handles in the heap).
  458.     aWindowObject = (WindowObject)NewHandle(sizeof(WindowObjectRecord));
  459.     
  460.     if(aWindowObject != NULL)
  461.     {
  462.         (**aWindowObject).controller = NULL;
  463.         (**aWindowObject).ObjectType = kMovieControllerObject;
  464.         
  465.         SetWRefCon(theWindow, (long)aWindowObject);        // store a ref to the record/handle into the window
  466.     }
  467.     
  468.     return aWindowObject;
  469. }
  470.  
  471.  
  472. // ______________________________________________________________________
  473. void HandleKeyPress(EventRecord *theEvent)
  474. {
  475.     char aKey;
  476.     
  477.     aKey = theEvent->message & charCodeMask;
  478.     
  479.     if(theEvent->modifiers & cmdKey)        // command key down?
  480.     {
  481.         HandleMenuCommand(MenuKey(aKey));
  482.     }
  483. }
  484.  
  485.  
  486. // ______________________________________________________________________
  487. void ShowAboutDialogBox(void)
  488. {
  489.     DialogPtr aDialog;
  490.     short         itemHit;
  491.     FontInfo    aFontInfo;
  492.     GrafPtr        aSavedPort;
  493.     
  494.     GetPort(&aSavedPort);
  495.     aDialog = GetNewDialog(kAboutBox, NULL, (WindowPtr) - 1L); DebugAssert(aDialog != NULL);
  496.     SetPort(aDialog);
  497.  
  498.     // Change font to Geneva, 9pt, bold, just for the sake of it...
  499.     TextFont(applFont); TextSize(9); TextFace(bold);
  500.     GetFontInfo(&aFontInfo);
  501.     
  502.     (*((DialogPeek)aDialog)->textH)->txFont = applFont;
  503.     (*((DialogPeek)aDialog)->textH)->txSize = 9;
  504.     (*((DialogPeek)aDialog)->textH)->lineHeight = aFontInfo.ascent + aFontInfo.descent + aFontInfo.leading;
  505.     (*((DialogPeek)aDialog)->textH)->fontAscent = aFontInfo.ascent;
  506.  
  507.     SetDialogDefaultItem(aDialog, 1);
  508.         
  509.     do
  510.     {
  511.         ModalDialog(NULL, &itemHit);
  512.     } while(itemHit != ok);
  513.     
  514.     SetPort(aSavedPort);
  515.     DisposeDialog(aDialog);  DebugAssert(MemError() == noErr);
  516. }
  517.  
  518.  
  519. // ______________________________________________________________________
  520. void ShowWarning(Str255 theMessage, OSErr theErr)
  521. {
  522.     Str255 errString;
  523.     
  524.     NumToString(theErr, errString);
  525.     ParamText("\pWarning!", theMessage, theErr ? errString:  NULL, NULL);
  526.     Alert(kAlertError, NULL);
  527. }
  528.  
  529.  
  530. // MOVIE RELATED TOOLBOX FUNCTIONS
  531.  
  532. // ______________________________________________________________________
  533. MovieController  GetMCFromFrontWindow(void)
  534. {
  535.     MovieController     mc = NULL;
  536.     WindowRef             aWindow = NULL;
  537.     WindowObject        aWindowObject = NULL;
  538.     Movie                    aMovie = NULL;
  539.     OSErr                    anErr = noErr;
  540.     OSType                    aType = NULL;
  541.  
  542.     if( ( aWindow = FrontWindow() ) == NULL )
  543.         return NULL;
  544.  
  545.     if( !IsAppWindow(aWindow) )
  546.         return NULL;
  547.             
  548.     aWindowObject = (WindowObject)GetWRefCon(aWindow);
  549.     if(aWindowObject == NULL)
  550.         return NULL;
  551.         
  552.     MoveHHi((Handle)aWindowObject); HLock((Handle)aWindowObject);
  553.  
  554.     // Test if this is indeed a movie controller, and not an otherwise valid pointer (non-NULL value)
  555.     if(!IsWindowObjectOurs(aWindowObject))
  556.         return NULL;
  557.         
  558.     mc = (**aWindowObject).controller;
  559.     HUnlock((Handle)aWindowObject);
  560.     
  561.     return mc;
  562. }
  563.  
  564.  
  565. // ______________________________________________________________________
  566. Boolean IsWindowObjectOurs(WindowObject theObject)
  567. {
  568.     OSType        aType = NULL;
  569.     
  570.     aType = (**theObject).ObjectType;
  571.     if(aType == kMovieControllerObject)
  572.         return true;
  573.     else return false;
  574. }
  575.  
  576.  
  577.  
  578. // ______________________________________________________________________
  579. Boolean DoCreateNewMovie(void)
  580. {
  581.     Movie aMovie = NULL;
  582.     
  583.     aMovie = NewMovie(newMovieActive); DebugAssert(aMovie != NULL);
  584.     if(aMovie == NULL)
  585.         return false;
  586.     
  587.     if(!DoCreateMovieWindow(aMovie))
  588.         return false;
  589.     else    
  590.         return true;
  591. }
  592.  
  593.  
  594. // ______________________________________________________________________
  595. Boolean DoCreateMovieWindow(Movie theMovie)
  596. {
  597.     Rect                         aRect = kDefaultWinRect;
  598.     WindowRef            aWindow = NULL;
  599.     MovieController    mc = NULL;
  600.     WindowObject        aWindowObject = NULL;
  601.     GrafPtr                    aSavedPort;
  602.     short                    aRefNum;
  603.     short                    aResID;
  604.     FSSpec                    aFileFSSpec;
  605.     
  606.     aFileFSSpec.vRefNum = 0;            // we want to use the FSSpec later
  607.  
  608.     GetPort(&aSavedPort);
  609.     aWindow = CreateMovieWindow(&aRect, gWindowTitle);
  610.     SetPort((GrafPtr) aWindow);
  611.     
  612.     if(aWindow == NULL)
  613.         return false;
  614.     
  615.     aWindowObject = CreateWindowObject(aWindow);
  616.     if(aWindowObject == NULL)
  617.         return false;
  618.  
  619. //If we don't get a movie, call the internal QTUGetMovie that will get us one.
  620.     if(theMovie == NULL)
  621.     {
  622.         theMovie = QTUGetMovie(&aFileFSSpec, &aRefNum, &aResID);  DebugAssert(theMovie != NULL);
  623.         if(theMovie == NULL)  // User selected cancel, or otherwise something bad happened.
  624.             return false;
  625.             
  626.         // Add the FSSpec, refnum and resid values to the window object (we need these when we save the movie).
  627.         (**aWindowObject).FileFSSpec = aFileFSSpec;
  628.         (**aWindowObject).FileRefNum = aRefNum;
  629.         (**aWindowObject).FileResID = aResID;
  630.  
  631.         // Get movie title and set this to the window title.
  632.         SetWTitle(aWindow, aFileFSSpec.name);
  633.     }
  634.     
  635.     SetMovieGWorld(theMovie, (CGrafPtr)aWindow, 0);        // make sure the movie uses the window GWorld at all situations
  636.     mc = SetupMovieWindowWithController(theMovie, aWindow);
  637.     
  638.     ShowWindow(aWindow);
  639.     SelectWindow(aWindow);                                    // make it front-most as it's just created
  640.     InvalRect( &((GrafPtr)aWindow)->portRect);
  641.     
  642.     MCEnableEditing(mc, true);                                // enable the default movie controller editing
  643.     
  644.     SetPort(aSavedPort);
  645.     return true;
  646. }
  647.  
  648.  
  649. // ______________________________________________________________________
  650. MovieController SetupMovieWindowWithController(Movie theMovie, WindowRef theWindow)
  651. {
  652.     MovieController     mc;
  653.     Rect                        aRect;
  654.     GrafPtr                    aSavedPort;
  655.     WindowObject        aWindowObject;
  656.     short                    aMovieWidth;
  657.     short                    aMovieHeight;
  658.     
  659.     DebugAssert(theMovie != NULL); 
  660.     DebugAssert(theWindow != NULL);
  661.     
  662.     aWindowObject = (WindowObject)GetWRefCon(theWindow);            // Get our window specific data.
  663.     if(!IsWindowObjectOurs(aWindowObject))
  664.         return NULL;                                                                                // Quick sanity test of the window created.
  665.     GetPort(&aSavedPort);
  666.     SetPort( (GrafPtr)theWindow);
  667.     
  668. // Resize the movie bounding rect.
  669.     GetMovieBox(theMovie, &aRect);     SetMovieBox(theMovie, &aRect);
  670.  
  671. // Create the movie controller.    
  672.     mc = NewMovieController(theMovie, &aRect, gMCFlags);
  673.     if(mc == NULL)
  674.         return NULL;
  675.     MCGetControllerBoundsRect(mc, &aRect);
  676.     
  677. // Add grow box for the movie controller and also an action filter that resizes the controllers
  678.     MCDoAction(mc, mcActionSetGrowBoxBounds, &kLimitRect);
  679.     MCSetActionFilterWithRefCon(mc, 
  680.                                                     NewMCActionFilterWithRefConProc(QTUResizeMCActionFilter),
  681.                                                     (long) theWindow);
  682.                                                     
  683.     // Check if the bounding rects are sane.
  684.     aMovieWidth = aRect.right - aRect.left;
  685.     aMovieHeight = aRect.bottom - aRect.top;
  686.     
  687.     aRect.top = aRect.left  = 0;
  688.     aRect.right = aMovieWidth;
  689.     aRect.bottom = aMovieHeight;
  690.     
  691.     // Resize the window as well.
  692.     SizeWindow(theWindow, aMovieWidth, aMovieHeight, true);
  693.     MoveWindow(theWindow, kDefaultX, kDefaultY, false);                                
  694.  
  695.     SetPort(aSavedPort);
  696.  
  697.     // Add any additional controller functionality.
  698.     AddControllerFunctionality(mc);
  699.  
  700.     // Save important stuff into the window object     and the original size of the movie
  701.     {    
  702.     Rect aRect;
  703.     OSErr anErr;
  704.  
  705.     (**aWindowObject).controller = mc;
  706.     anErr = MCGetControllerBoundsRect(mc, &aRect); DebugAssert(anErr == noErr);
  707.     (**aWindowObject).originalSize = aRect;
  708.     }
  709.     
  710.     return mc;
  711. }
  712.  
  713.  
  714. // ______________________________________________________________________
  715. Boolean DoUpdateMovieFile(WindowRef theWindow)
  716. {
  717.     Movie                     aMovie = NULL;
  718.     WindowObject        aWindowObject = NULL;
  719.     MovieController    mc = NULL;
  720.     OSErr                    anErr;
  721.     
  722.     if ( (theWindow == NULL) || !IsAppWindow(theWindow) )
  723.         return false;
  724.         
  725.     aWindowObject = (WindowObject)GetWRefCon(theWindow); DebugAssert(aWindowObject != NULL);
  726.     mc = (**aWindowObject).controller; DebugAssert(mc != NULL);
  727.     
  728.     if( !(IsWindowObjectOurs(aWindowObject)) && (mc == NULL) )
  729.         return false;
  730.     
  731.     aMovie = MCGetMovie(mc); DebugAssert(aMovie != NULL);
  732.     if(aMovie == NULL)
  733.         return false;
  734.         
  735.     if( (**aWindowObject).FileRefNum == -1)                    // brand new movie, no file attached to it.
  736.     {
  737.         if ( QTUSaveMovie(aMovie) != noErr)
  738.             return false;    
  739.     }
  740.     else                                                                            // we have an existing file, just update the movie resource
  741.     {
  742.         // Open the movie resource file, update the resource, and then close it again!
  743.         anErr = OpenMovieFile(& (**aWindowObject).FileFSSpec, & (**aWindowObject).FileRefNum, fsRdWrPerm);
  744.         DebugAssert(anErr == noErr);
  745.         if(anErr != noErr)
  746.             return false;
  747.         
  748.         anErr = UpdateMovieResource(aMovie, (**aWindowObject).FileRefNum, (**aWindowObject).FileResID, NULL);
  749.         DebugAssert(anErr == noErr);
  750.         
  751.         CloseMovieFile( (**aWindowObject).FileRefNum );
  752.     }
  753.     
  754.     if(anErr == noErr)
  755.         return true;
  756.     else
  757.         return false;
  758. }
  759.  
  760.  
  761. // ______________________________________________________________________
  762. void DoDestroyMovieWindow(WindowRef theWindow)
  763. {
  764.     Movie                     aMovie;
  765.     MovieController    mc;
  766.     WindowObject        aWindowObject;
  767.     
  768.     DebugAssert(theWindow != NULL); if(theWindow == NULL) return;
  769.     
  770.     aWindowObject =(WindowObject)GetWRefCon(theWindow);
  771.     MoveHHi((Handle)aWindowObject);
  772.     HLock((Handle)aWindowObject);
  773.     
  774.     if ( IsWindowObjectOurs(aWindowObject)) // our window?
  775.     {
  776.         mc = (**aWindowObject).controller;
  777.         aMovie = MCGetMovie(mc);
  778.         
  779.         if(aMovie != NULL)
  780.             DisposeMovie(aMovie); DebugAssert(MemError() == noErr);
  781.     
  782.         if(mc != NULL)
  783.             DisposeMovieController(mc); DebugAssert(MemError() == noErr);
  784.     
  785.         if( (**aWindowObject).FileRefNum != -1)
  786.             CloseMovieFile((**aWindowObject).FileRefNum);
  787.         
  788.         (**aWindowObject).ObjectType = NULL;
  789.         (**aWindowObject).controller = NULL;
  790.         (**aWindowObject).FileResID = NULL;
  791.         (**aWindowObject).FileRefNum = NULL;
  792.         
  793.         DisposeHandle((Handle)aWindowObject); DebugAssert(MemError() == noErr);
  794.         DisposeWindow(theWindow); DebugAssert(MemError() == noErr);
  795.         
  796.         CompactMem(0xFFFFFFFF);        //We might as well compact the mem here for getting better performance later.
  797.     }
  798. }
  799.  
  800.  
  801. // ______________________________________________________________________
  802. void DoActivateWindow(WindowRef theWindow, Boolean becomingActive)
  803. {
  804.     WindowObject         aWindowObject = NULL;
  805.     MovieController    mc = NULL;
  806.     GrafPtr                    aSavedPort = NULL;
  807.     
  808.     GetPort(&aSavedPort);
  809.     SetPort((GrafPtr)theWindow);
  810.     
  811.     if( (aWindowObject = (WindowObject)GetWRefCon(theWindow)) != NULL)
  812.     {
  813.         mc = (**aWindowObject).controller;
  814.         if( (IsWindowObjectOurs(aWindowObject)) && (mc != NULL) )
  815.             MCActivate(mc, theWindow, becomingActive);
  816.     }
  817.     SetPort(aSavedPort);
  818. }
  819.  
  820.  
  821.  
  822.